<?php
require_once(SMARTY_DIR."Smarty.class.php");

/**
 * Clase ContentManager
 * @author chalodrv@yahoo.es
 */
class ContentManager
{
	/**
	 * Variables
	 */
	var $tpl;
	var $lang;
	var $langmsg;
	var $basePath;

	/**
	 * Constructor
	 * @param string $lang
	 */
	function __construct($lang = DEFAULT_LANGUAGE)
	{
		$this->tpl = new Smarty();
		$this->lang = $lang;
		require_once(LANG_DIR.$this->lang.".php");
		$this->langmsg = $langmsg;
	}
	
	/**
	 * Método assign
	 * @param string $field
	 * @param string $value
	 */
	function assign($field, $value)
	{
		$this->tpl->assign($field, $value);
	}

	/**
	 * Método display
	 * @param string $templateName
	 */
	function display($templateName)
	{
		$this->tpl->display($templateName);
	}
	
	/**
	 * Método fetch
	 * @param string $templateName
	 */
	function fetch($templateName)
	{
		return $this->tpl->fetch($templateName);
	}

	/**
	 * Método adminInitiate
	 */
	function adminInitiate($subsection="dashboard")
	{
		$menuoptions = array(
			array(
				"namel1" => "Panel de",
				"namel2" => "control",
				"url" => ADMIN_URL,
				"current" => ($subsection == "dashboard" ? 1 : 0)
			),
			array(
				"namel1" => "Comentarios",
				"namel2" => "del blog",
				"url" => ADMIN_URL."blogcomments/",
				"current" => ($subsection == "blogcomments" ? 1 : 0)
			),
			array(
				"namel1" => "Comentarios",
				"namel2" => "de noticias",
				"url" => ADMIN_URL."newscomments/",
				"current" => ($subsection == "newscomments" ? 1 : 0)
			)
		);
		$this->tpl->assign("menuoptions", $menuoptions);

		$this->tpl->assign("csspath", CSS_URL);
	}

	/**
	 * Método initiate
	 */
	function initiate($ssl=false)
	{
		// Menú
		$menuoptions = array();
		$menuoptionsnames = array("home", "tours", "semana-santa-2012", "verano-2012", "alojamientos", "reservations", "info", "faq", "gallery", "blog", "contact");
		foreach ($menuoptionsnames as $menuoptionname) {
			$menuoptions[$menuoptionname] = array(
				"name" => $this->langmsg["menu-".$menuoptionname],
				"alias" => $menuoptionname,
				"url" => ($menuoptionname == "reservations" ? SECURE_URL : BASE_URL).$this->lang."/".($menuoptionname != "home" ? $menuoptionname."/" : "")
			);
		}
		$this->tpl->assign("menuoptions", $menuoptions);

		// Idiomas
		$languages = array();
		$languagesnames = unserialize(AVAILABLE_LANGUAGES);
		foreach ($languagesnames as $languagename) {
			$languages[$languagename] = array(
				"name" => $this->langmsg["languages"][$languagename],
				"code" => $languagename,
				"url" => BASE_URL.$languagename."/"
			);
		}
		$this->tpl->assign("languages", $languages);
		
		// Mapa del sitio y Aviso legal
		$this->tpl->assign("legalnoticetext", $this->langmsg["legalnotice"]);
		$this->tpl->assign("legalnoticeurl", Utils::generateSEOUrl($this->lang, "legalnotice"));
		$this->tpl->assign("linkstext", $this->langmsg["links"]);
		$this->tpl->assign("linksurl", Utils::generateSEOUrl($this->lang, "links"));
		$this->tpl->assign("sitemaptext", $this->langmsg["sitemap"]);
		$this->tpl->assign("sitemapurl", Utils::generateSEOUrl($this->lang, "sitemap"));

		// Base path
		$basePath = $ssl ? SECURE_URL : BASE_URL;
		$this->basePath = $basePath;

		// Asignamos los valores
		$this->tpl->assign("lang", $this->lang);
		//$this->tpl->assign("keywords", KEYWORDS);
		//$this->tpl->assign("pagedescription", PAGE_DESCRIPTION);
		$this->tpl->assign("sitename", SITE_NAME);
		$this->tpl->assign("sitenamepart1", SITE_NAME_PART1);
		$this->tpl->assign("sitenamepart2", SITE_NAME_PART2);
		$this->tpl->assign("basepath", $basePath);
		$this->tpl->assign("imagespath", $basePath.IMAGES_URL);
		$this->tpl->assign("gallerypath", $basePath.GALLERY_URL);
		$this->tpl->assign("csspath", $basePath.CSS_URL);
		$this->tpl->assign("jspath", $basePath.JS_URL);
		$this->tpl->assign("includespath", $basePath.INCLUDES_URL);
		$this->tpl->assign("socialiconsize", SOCIAL_ICON_SIZE);
		$this->tpl->assign("slogan", $this->langmsg["slogan"]);
		$this->tpl->assign("homeurl", $menuoptions["home"]["url"]);
		$this->tpl->assign("contactemail", CONTACT_EMAIL);
		$this->tpl->assign("contactphone", CONTACT_PHONE);
		$this->tpl->assign("captchaurl", $basePath."captcha.php");
	}

	/**
	 * Método generateComboOptions
	 * @param string $type
	 * @param string $selectedItem
	 */
	function generateComboOptions($type, $selectedItem="")
	{
		$code = "";
		switch ($type) {
			// Días del mes
			case "days":
				for ($i=1; $i<=31; $i++) {
					$code .= "<option value=\"".(strlen($i)<2 ? "0$i" : $i)."\"".($selectedItem == $i ? " selected=\"selected\"" : "").">".$i."</option>\n";
				}
				break;
			// Meses
			case "months":
				$months = array(
					1 => $this->langmsg["january"],
					2 => $this->langmsg["february"],
					3 => $this->langmsg["march"],
					4 => $this->langmsg["april"],
					5 => $this->langmsg["may"],
					6 => $this->langmsg["june"],
					7 => $this->langmsg["july"],
					8 => $this->langmsg["august"],
					9 => $this->langmsg["september"],
					10 => $this->langmsg["october"],
					11 => $this->langmsg["november"],
					12 => $this->langmsg["december"]
				);
				foreach ($months as $number => $name) {
					$code .= "<option value=\"".(strlen($number)<2 ? "0$number" : $number)."\"".($selectedItem == $number ? " selected=\"selected\"" : "").">".$name."</option>\n";
				}
				break;
			// Años
			case "years":
				$currentYear = date("Y");
				for ($i=$currentYear; $i<=($currentYear+5); $i++) {
					$code .= "<option value=\"".$i."\"".($selectedItem == $i ? " selected=\"selected\"" : "").">".$i."</option>\n";
				}
				break;
		}
		return $code;
	}

	/**
	 * Método generateShareLinks
	 * @param string $linkUrl
	 */
	function generateShareLinks($linkUrl)
	{
		$this->assign("sharelinks", $GLOBALS["SHARE_LINKS"]);
		$this->assign("linkurl", $linkUrl);
		return $this->fetch("sharelinks.tpl");
	}

	/**
	 * Método multipleAssign
	 * @param array $varNames
	 */
	function multipleAssign($varNames)
	{
		if (!is_array($varNames)) {
			return false;
		} else {
			foreach ($varNames as $varName) {
				$this->assign($varName, $this->langmsg[$varName]);
			}
			return true;
		}
	}

	/**
	 * Método getBasePath
	 */
	function getBasePath()
	{
		return $this->basePath;
	}

	function generatePaginationLinks($section, $subsection="", $numRows=0, $rowsPerPage=0, $lang=DEFAULT_LANGUAGE)
	{
		$maxPages = ceil($numRows / $rowsPerPage);
		$page = isset($_GET["page"]) ? $_GET["page"] : 1;
		if ($page < 1) $page = 1;
		if ($page > $maxPages) $page = $maxPages;

		$navPages = array();
		if ($page > 1) {
			$navPages[] = array(
				"number" => "<",
				"link" => 1,
				"url" => Utils::getBaseUrl($lang, $section, $subsection).($page - 1)."/"
			);
		}
		for ($i=1; $i<=$maxPages; $i++) {
			$navPages[] = array(
				"number" => $i,
				"link" => $i == $page ? 0 : 1,
				"url" => Utils::getBaseUrl($lang, $section, $subsection).$i."/"
			);
		}
		if ($page < $maxPages) {
			$navPages[] = array(
				"number" => ">",
				"link" => 1,
				"url" => Utils::getBaseUrl($lang, $section, $subsection).($page + 1)."/"
			);
		}

		$this->assign("navPages", $navPages);
		return $this->fetch("pagination.tpl");
	}
}
?>
